<template>
  <div>
    <div class="app-container" v-show="showPage==1">
      <div class="filter-container">
        <el-input
          v-model="keyword"
          placeholder="SN/MAC"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
        <el-select v-model="devType" clearable placeholder="设备类型" class="filter-item">
          <el-option
            v-for="item in deviceTypes"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button class="filter-item" type="info" icon="el-icon-search" @click="resetQuery">重置</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >Search</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-top"
          @click="showUpdatePage"
        >设备升级</el-button>
      </div>

      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        @sort-change="sortChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column v-if="false" label="Date" width="150px" align="center">
          <template slot-scope="{row}">
            <span>{{ row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="设备型号" width="110px" align="center">
          <template slot-scope="{row}">
            <span>{{ row.devModel }}</span>
          </template>
        </el-table-column>
        <el-table-column label="设备类型" align="center">
          <template slot-scope="{row}">
            <span>{{ row.devType }}</span>
          </template>
        </el-table-column>
        <el-table-column label="硬件版本" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.hVersion }}</span>
          </template>
        </el-table-column>
        <el-table-column label="软件版本" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.sVersion }}</span>
          </template>
        </el-table-column>
        <el-table-column label="SN" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.sn }}</span>
          </template>
        </el-table-column>
        <el-table-column label="MAC地址" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.mac }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="showDetail(row)">Edit</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <el-dialog title="设备升级" :visible.sync="dialogFormVisible">
      <div class="filter-container">
        <el-input
          v-model="updateKeyword"
          placeholder="SN/MAC"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleFilter2"
        />
        <el-select v-model="updateDevType" clearable placeholder="设备类型" class="filter-item">
          <el-option
            v-for="item in deviceTypes"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button
          class="filter-item"
          type="info"
          icon="el-icon-search"
          @click="resetUpdateQuery"
        >重置</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter2"
        >Search</el-button>
      </div>

      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="listUpdate"
        highlight-current-row
        @current-change="handleCurrentChange"
        border
        fit
        style="width: 100%;"
      >
        <el-table-column label="设备类型" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.devType }}</span>
          </template>
        </el-table-column>
        <el-table-column label="升级包名称" width="110px" align="center">
          <template slot-scope="{row}">
            <span>{{ row.packageName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="版本号" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.packageVersion }}</span>
          </template>
        </el-table-column>
        <el-table-column label="升级包长度" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.packageSize }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="packageTotal>0"
        :total="packageTotal"
        :page.sync="listUpdateQuery.page"
        :limit.sync="listUpdateQuery.limit"
        @pagination="getPackageList"
      />

      <div>
        <el-row>
          <el-col :span="6">
            <el-radio v-model="radioA" label="1">立即升级</el-radio>
          </el-col>
          <el-col :span="6">
            <el-radio v-model="radioA" label="2">定时升级</el-radio>
          </el-col>
        </el-row>
        <el-row>
          <el-date-picker v-show="showTimeSelector"
            size="small"
            v-model="updateTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm" 
            format="yyyy-MM-dd HH:mm"
            placeholder="选择日期"
          />
        </el-row>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="upgrade()">提交</el-button>
      </div>
    </el-dialog>
    <div class="createPost-container" v-show="showPage==2">
      <sticky :z-index="10" :class-name="'sub-navbar-draft '">
        <el-button
          type="primary"
          @click="back2Main"
          style="margin-left:15px"
          icon="el-icon-arrow-left"
        >返回</el-button>
      </sticky>
      <el-form class="form-container">
        <div class="createPost-main-container" style>
          <el-row>
            <el-col :span="24">
              <el-row>
                <p class="row-title">系统信息</p>
              </el-row>
              <div class="postInfo-container">
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="设备型号:">{{item.devModel}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="设备类型:">{{item.devType}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="硬件版本:">{{item.hVersion}}</el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="软件版本:">{{item.sVersion}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item
                      label="发布日期:"
                    >{{ item.publishDt | parseTime('{y}-{m}-{d} {h}:{i}') }}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="MAC地址:">{{item.mac}}</el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="SN:">{{item.SN}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="运行时间:">{{item.run_dt}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="服务器域名或IP1:">{{item.ip1}}</el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="ip2:">{{item.ip2}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="机器码:">{{item.machineCode}}</el-form-item>
                  </el-col>
                </el-row>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-divider class="divider"></el-divider>
        <div class="createPost-main-container">
          <el-row>
            <el-col :span="24">
              <el-row>
                <p class="row-title">网络概况/资源使用率</p>
              </el-row>
              <div class="postInfo-container">
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="设备型号:">{{item.devModel}}</el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="设备类型:">{{item.devType}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="硬件版本:">{{item.hVersion}}</el-form-item>
                  </el-col>
                </el-row>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-divider class="divider"></el-divider>
        <div class="createPost-main-container">
          <el-row>
            <el-col :span="24">
              <el-row>
                <p class="row-title">拨号联网配置</p>
              </el-row>
              <div class="postInfo-container">
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="设备型号:">{{item.devModel}}</el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="设备类型:">{{item.devType}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="硬件版本:">{{item.hVersion}}</el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-button type="success">保存</el-button>
                </el-row>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-divider class="divider"></el-divider>
        <div class="createPost-main-container">
          <el-row>
            <el-col :span="24">
              <el-row>
                <p class="row-title">wifi设置</p>
              </el-row>
              <div class="postInfo-container">
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="设备型号:">{{item.devModel}}</el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="设备类型:">{{item.devType}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="硬件版本:">{{item.hVersion}}</el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-button type="success">保存</el-button>
                </el-row>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-divider class="divider"></el-divider>
        <div class="createPost-main-container">
          <el-row>
            <el-col :span="24">
              <el-row>
                <p class="row-title">网络设置</p>
              </el-row>
              <div class="postInfo-container">
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="设备型号:">{{item.devModel}}</el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="设备类型:">{{item.devType}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="硬件版本:">{{item.hVersion}}</el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-button type="success">保存</el-button>
                </el-row>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-divider class="divider"></el-divider>
        <div class="createPost-main-container">
          <el-row>
            <el-col :span="24">
              <el-row>
                <p class="row-title">系统设置</p>
              </el-row>
              <div class="postInfo-container">
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="设备型号:">{{item.devModel}}</el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="设备类型:">{{item.devType}}</el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="硬件版本:">{{item.hVersion}}</el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-button type="success">保存</el-button>
                </el-row>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { listByPage, selectById } from "@/api/baseService";
import { getDictByType } from "@/api/dict";
import { updateDev } from "@/api/upgradeDev";
import Sticky from "@/components/Sticky"; // 粘性header组件
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

export default {
  name: "DevInfo",
  components: { Pagination, Sticky },
  data() {
    return {
      tableKey: 0,
      list: null,
      listUpdate: null,
      currentPackageId: null,
      multipleSelection: [],
      total: 0,
      packageTotal: 0,
      showPage: 1,
      listLoading: true,
      keyword: null,
      updateKeyword: null,
      devModel: "",
      devType: "",
      updateDevType: "",
      selectedUpdatePage: null,
      dialogFormVisible: false,
      deviceTypes: {},
      radioA: "1",
      updateTime: null,
      showTimeSelector: false,
      listQuery: {
        page: 1,
        limit: 20,
        params: null
      },
      listUpdateQuery: {
        page: 1,
        limit: 20,
        params: null
      },
      item: {
        devModel: null,
        devType: null,
        mac: null
      }
    };
  },
  created() {
    this.getList();
    getDictByType({ dictType: "device_type" }).then(response => {
      this.deviceTypes = response.data;
    });
  },
  watch: {
    radioA(val, oldVal) {
      if (val == "2") {
        this.showTimeSelector = true;
      } else {
        this.showTimeSelector = false;
        this.updateTime = null;
      }
    }
  },
  methods: {
    //返回列表页
    back2Main() {
      this.showPage = 1;
      this.getList();
    },
    //获取列表
    getList() {
      this.listLoading = true;
      this.listQuery.params = {
        keyword: this.keyword,
        devModel: this.devModel + "",
        devType: this.devType + ""
      };
      listByPage("devInfo/listByPage", this.listQuery).then(response => {
        this.list = response.result.rows;
        this.total = response.result.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleFilter2() {
      this.listUpdateQuery.page = 1;
      this.getPackageList();
    },
    //重置
    resetQuery() {
      this.listQuery.page = 1;
      this.keyword = "";
      this.devModel = "";
      this.devType = "";
      this.getList();
    },
    resetUpdateQuery() {
      this.listUpdateQuery.page = 1;
      this.UpdateKeyword = "";
      this.updateDevType = "";
      this.getPackageList();
    },
    showUpdatePage() {
      this.dialogFormVisible = true;
      this.getPackageList();
    },
    getPackageList() {
      this.listUpdateQuery.params = {
        keyword: this.updateKeyword,
        devType: this.updateDevType
      };
      listByPage("upgrade/listByPage", this.listUpdateQuery).then(response => {
        if (response.code == 0) {
          this.listUpdate = response.result.rows;
          this.packageTotal = response.result.total;
          this.listLoading = false;
        }
      });
    },
    //升级设备
    upgrade() {
      var params = {
        devIds: this.multipleSelection,
        packageId: this.currentPackageId,
        updateMethod: this.radioA,
        updateTime: this.updateTime
      };
      updateDev(params).then(response => {});
    },
    sortChange(data) {
      const { prop, order } = data;
    },
    handleCurrentChange(val) {
      if (val) {
        this.currentPackageId = val.packageId;
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      if (val) {
        val.forEach(element => {
          this.multipleSelection.push(element.devId);
        });
      }
    },
    showDetail(row) {
      this.showPage = 2;
      var params = {
        devId: row.devId
      };
      selectById("devInfo/selectById", params).then(response => {
        this.item = response.data;
      });
    },
    formatJson(filterVal) {
      return this.list.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
.divider {
  margin-left: 40px;
}
.row-title {
  font-size: 18px;
  color: #909399;
}
</style>